package com.bjpowernode.mvc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.bjpowernode.common.enums.RespCode;
import com.bjpowernode.db.domain.SysUser;
import com.bjpowernode.db.result.BidResult;
import com.bjpowernode.mvc.convert.BidConvert;
import com.bjpowernode.mvc.model.param.BidParam;
import com.bjpowernode.mvc.model.result.CommonResult;
import com.bjpowernode.mvc.model.dto.BidRankDTO;
import com.bjpowernode.mvc.model.vo.BidRankVO;
import com.bjpowernode.mvc.model.vo.BidResultVO;
import com.bjpowernode.mvc.service.inter.BidService;
import com.bjpowernode.mvc.service.inter.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RequiredArgsConstructor
@RestController
public class BidController {

    private final BidService bidService;
    private final UserService userService;

    private final BidConvert bidConvert;

    //获取投资排行榜数据
    @GetMapping("/bid/rank")
    public CommonResult showMoneyRank() {

        //从Redis获取排行榜的数据
        List<BidRankDTO> bidRankList = bidService.queryMoneyRank();

        //DTO转为VO
        List<BidRankVO> bidRankVoList = bidConvert.toBidRankVo(bidRankList);

        return CommonResult.ok(bidRankVoList);
    }

    //投资、购买理财产品  参数需要进行修改
    @PostMapping("/bid/buy")
    public CommonResult buyProduct(
            @RequestBody @Validated BidParam bidParam,
            @RequestHeader("uid") Integer uid
    ) {
        //doubleValue()是BigDecimal类的一个方法，作用是将BigDecimal对象转换为double类型
        //bidMoney不为null  bidMoney必须大于等于100 bidMoney必须是100的整数倍
        Integer productId = bidParam.getProductId();
        BigDecimal bidMoney = bidParam.getBidMoney();

        //1、参数验证
        if (bidMoney.doubleValue() >= 100 && bidMoney.doubleValue() % 100 == 0) {
            //2、判断是否实名认证
            SysUser sysUser = userService.queryById(uid);
            //StrUtil.isBlank()检查一个字符串是否为空或只包含空白字符
            if (sysUser == null || StrUtil.isBlank(sysUser.getName())) {
                return new CommonResult(RespCode.USER_RELNAME_REQUIRED);
            }

            //3、投资  购买理财产品
            String buyResult = bidService.buyProduct(uid, productId, bidMoney);
            //valueOf()方法：将一个字符串转换为一个枚举类
            return new CommonResult(RespCode.valueOf(buyResult));
        }
        return new CommonResult(RespCode.REQUEST_PARAM_ERR);
    }

    /**
     * 查询投资记录信息
     *
     * @param uid
     * @return
     */

    @GetMapping("/bid/info")
    public CommonResult queryBidInfo(
            @RequestHeader("uid") Integer uid,
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize
    ) {
        //从第几页开始，查询多少条数据
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        pageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? 6 : pageSize;

        //1、查询最近投资信息
        List<BidResult> bidResultList = bidService.queryBidByUid(uid, pageNo, pageSize);

        //2、转VO
        List<BidResultVO> bidResultVOList = BeanUtil.copyToList(bidResultList, BidResultVO.class);

        return CommonResult.ok(bidResultVOList);
    }

}
